<?php


namespace catchAdmin\project\controller;


use app\common\utils\Str;
use catchAdmin\project\request\NodeRequest;
use catcher\base\CatchController;
use catcher\base\CatchRequest;
use catcher\CatchResponse;
use catcher\exceptions\CatchException;
use catcher\exceptions\FailedException;
use catcher\library\client\Response;
use catcher\Utils;
use think\exception\ValidateException;

class Node extends CatchController
{
    protected ?\app\common\business\Node $business = null;

    public function __construct()
    {

        $this->business = new \app\common\business\Node();

    }

    public function index(CatchRequest $catchRequest): \think\response\Json
    {

        $result = $this->business->getList();
        return CatchResponse::paginate($result);
    }

    public function save(CatchRequest $request): \think\response\Json
    {
        try {
            $postData = $request->param();
            validate(NodeRequest::class)->scene("create")->check($postData);
            $projectInfo = (new \app\common\business\Project())->getProjectInfo($postData["project_id"]);
            if (empty($projectInfo)) {
                throw new FailedException("问卷不存在");
            }
            $nodeInfo = $this->business->getExists((int)$postData["is_whole_hospital"], (int)$postData["node_id"], (array)$postData["apply_dept"]);

            if (!empty($nodeInfo)) {
                if ($postData["is_whole_hospital"] === 1) {
                    $errMessage = sprintf("%s节点的全院性问卷已经在编号%s上绑定过了", $nodeInfo["node_name"], $nodeInfo["node_code"]);
                } else {
                    $errMessage = sprintf("您勾选的科室已经在编号%s绑定过了", $nodeInfo["node_code"]);
                }
                throw new FailedException($errMessage);
            }
            $postData["node_code"] = Str::alphaID(6);
            $postData["project_uuid"] = $projectInfo["id"];
            $postData["project_name"] = $projectInfo["name"];
            $postData["send_count"] = $postData["answer_count"] = 0;
            return CatchResponse::success($this->business->createData($postData));

        } catch (ValidateException $validateException) {
            throw new ValidateException($validateException->getMessage());
        } catch (\Exception $exception) {
            throw new FailedException($exception->getMessage());
        }

    }

    public function update($id, CatchRequest $request): \think\response\Json
    {
        try {
            $postData = $request->put();
            validate(NodeRequest::class)->scene('update')->check($postData);
            $condition[] = ["id", '<>', $id];
            $nodeInfo = $this->business->getInfo($id);
            if (empty($nodeInfo)) {
                throw new FailedException("数据不存在");
            }
            $isExists = $this->business->getExists((int)$postData["is_whole_hospital"], (int)$postData["node_id"], (array)$postData["apply_dept"], $condition);
            if (!empty($isExists)) {
                if ($postData["is_whole_hospital"] === 1) {
                    $errMessage = sprintf("%s节点的全院性问卷已经在编号%s上绑定过了", $isExists["node_name"], $isExists["node_code"]);
                } else {
                    $errMessage = sprintf("您勾选的科室已经在编号%s绑定过了", $isExists["node_code"]);
                }
                throw new FailedException($errMessage);
            }
            $result = $nodeInfo->save($postData);
            return CatchResponse::success($result);

        } catch (ValidateException $validateException) {
            throw new ValidateException($validateException->getMessage());
        }
    }

    public function delete($id): \think\response\Json
    {
        return CatchResponse::success(\app\common\model\Node::destroy($id));
    }
}